<template>
  <div class="main">
    <eagle-input
      label="风险位置名称"
      name="area"
      :value="data.RiskUnitName"
      required
      @input="ChangeVal"
      isInputText
    ></eagle-input>
    <eagle-input
      name="calendar"
      v-model="data.OrgName"
      label="所属区域"
      placeholder="请选择"
      readonly
      required
      @click-input="goRiskLocationItem"
    >
      <template #button>
        <eagle-icon name="cross" @click="clearOrgCode" />
      </template>
    </eagle-input>
    <eagle-input
      ame="calendar"
      :value="data.ManagerName"
      label="部门负责人"
      placeholder="请选择"
      readonly
      @click-input="UserChooseShow=true"
    >
      <template #button
        ><eagle-icon name="cross" @click="clearManager"
      /></template>
    </eagle-input>
    <eagle-input
      name="calendar"
      :value="data.LeaderName"
      label="分管领导"
      placeholder="请选择"
      readonly
      @click-input="leaderChooseShow=true"
    >
      <template #button
        ><eagle-icon name="cross" @click="clearLeader"
      /></template>
    </eagle-input>
    <div class="btns m-10">
      <van-button type="default" @click="_submit"> 保存 </van-button>
    </div>
    <UserChoose
      title="选择部门负责人"
      :show.sync="UserChooseShow"
      :isMulti="true"
      :tagData="tagData"
      @callback="_chooseUserCb"
    ></UserChoose>
    <UserChoose
      title="选择分管领导"
      :show.sync="leaderChooseShow"
      :selectedValue="data.Leader"
      @callback="_chooseLeaderUserCb"
    ></UserChoose>
    <DeptmentChoose
      title="选择部门"
      :show.sync="DeptmentShow"
      :selectedValue="data.OrgCode"
      @callback="_chooseDeptmentCb"
    ></DeptmentChoose>
  </div>
</template>

<script>
import UserChoose from "@/views/components/user/userChooseDialog.vue";
import DeptmentChoose from "@/views/components/department/departmentChooseDialog.vue";
import TeamChoose from "@/views/components/user/userTeamChooseDialog.vue";
import PostChoose from "@/views/components/user/userPostChooseDialog.vue";

export default {
  components: {
    UserChoose,
    DeptmentChoose,
    TeamChoose,
    PostChoose,
  },
  data() {
    return {
      menuList: [],
      mobile: "",
      message: "",
      value: "",
      radio: "1",
      checkbox: false,
      checkboxGroup: [],
      UserChooseShow: false,
      leaderChooseShow: false,
      DeptmentShow: false,
      TeamShow: false,
      PostShow: false,
      data: {},
      id: 0,
      tagData:[],
    };
  },

  created() {
    this.initData();
  },

  activated() {},
  methods: {
    ChangeVal(val) {
      this.data.RiskUnitName = val;
    },
    getSimpleDetail() {
      let _this = this;
      _this.$axios
      .get("/RiskUnit/GetSimpleDetail",{params:{id: _this.id}})
      .then((res)=>{
        _this.data = res.data;
        if(_this.data.Manager){
          let manageCodes = _this.data.Manager.split(",");
          let manageNames = _this.data.ManagerName.split(",");
          manageCodes.forEach((item,index,arr)=>{
            let user = {code:item,UserName:item,name:manageNames[index],ChnName:manageNames[index]};
            _this.tagData.push(user);
          })
        }
      })
      .catch((err) => {
        console.log("err", err);
      });
    },
    // 部门
    goRiskLocationItem() {
      this.DeptmentShow = true;
    },
    _chooseDeptmentCb(item) {
      this.$set(this.data, "OrgName", item.name || "");
      this.$set(this.data, "OrgCode", item.code || "");
      this.DeptmentShow = false;
    },
    clearOrgCode() {
      this.data.OrgCode = null;
      this.data.OrgName = null;
    },
    // 班组
    goTeamListItem() {
      this.TeamShow = true;
    },
    _chooseTeamCb(item) {
      this.data.TeamName = item.name || ""
      this.data.TeamCode = item.code || "";
      this.TeamShow = false;
    },
    clearTeam() {
      this.data.TeamName = null;
      this.data.TeamCode = null;
    },
    // 岗位
    goPostListItem() {
      this.PostShow = true;
    },
    _choosePostCb(item) {
      this.data.OrgPostName = item.name || ""
      this.data.OrgPostCode = item.code || "";
      this.PostShow = false;
    },
    clearPost() {
      this.data.OrgPostName = null;
      this.data.OrgPostCode = null;
    },
    // 人员
    _chooseUserCb(data) {
      
      let user = this.common.getCodeAndNames(data,{code:"UserName",name:"ChnName"});
      this.data.ManagerName = user.name || "";
      this.data.Manager = user.code || ""
    },
    _chooseLeaderUserCb(item){
      this.data.LeaderName = item.name || "";
      this.data.Leader = item.code || "";
    },
    clearManager() {
      
      this.data.Manager = null;
      this.data.ManagerName = null;
      this.tagData = [];
    },
    clearLeader() {
      this.data.Leader = null;
      this.data.LeaderName = null;
    },
    clearTechDirector() {
      this.data.TechDirector = null;
      this.data.TechDirectorName = null;
    },
    initData() {
      let _this = this;
      if (_this.$route.query.id) {
        _this.id = _this.$route.query.id;
      }
      if (_this.id > 0) {
        _this.getSimpleDetail();
      } else {
        _this.$axios
        .get("/RiskUnit/InitNewEntity",{params:{id: _this.id}})
        .then((res)=>{
          _this.data = res.data;
        })
        .catch((err) => {
          console.log("err", err);
        });
      }
    },
    // 保存
    _submit() {
      let _this = this;
      if (!_this.data.RiskUnitName) {
        _this.$toast("请输入风险位置名称");
        return;
      }
      if (!_this.data.OrgName) {
        _this.$toast("请选择所属区域");
        return;
      }
      _this.$axios
      .post("/RiskUnit/PostData",_this.data)
      .then((res)=>{
        if (res.success) {
          if (_this.$route.params.path) {
            _this.$router.push({
              name: "HiddenDangerArea",
              params: {
                path: _this.$route.params.path,
                id: _this.$route.params.id,
                key: _this.$route.params.key,
                secondId: _this.$route.params.secondId,
              },
            });
          } else {
            _this.$router.push("/Site_RiskUnit");
          }
        }
      })
      .catch((err) => {
        console.log("err", err);
      });
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  padding-top: 10px;
}
.input_text .van-field__control {
  color: #999 !important;
}
.popup-title {
  line-height: 35px;
  padding: 10px;
  font-size: 15px;
  padding-left: 20px;
}
</style>
